# == Schema Information
#
# Table name: categories # 产品分类
#
#  id                   :bigint(8)        not null, primary key
#  name(产品名称)           :string
#  alias(别名)            :string
#  parent_id(父级id)      :integer
#  lft(左边的id)           :integer          not null
#  rgt(右边的id)           :integer          not null
#  depth(深度)            :integer          default(0), not null
#  children_count(子级数量) :integer          default(0), not null
#  created_at           :datetime         not null
#  updated_at           :datetime         not null
#
# Indexes
#
#  index_categories_on_lft        (lft)
#  index_categories_on_parent_id  (parent_id)
#  index_categories_on_rgt        (rgt)
#

class Category < ApplicationRecord
  acts_as_nested_set

  has_many :products

  def summary_builder
    Jbuilder.new do |json|
      json.(self, :id, :name, :alias, :parent_id)
      json.parent_name self.parent ? self.parent.try(:name) : ''
      json.products_count self.products.count
    end.attributes!
  end

end
